$(function () {

    //初始化专业
    $.initSubject({
        element:'user-subject-box',//初始化专业select元素的ID
        context:'user-subject-context',//显示专业/科目名字内容ID
        initData:$('#subjectIds').val(),//初始化显示的专业数据，默认不设置
        removeCallback:function(parentId,subjectIds){//删除回调
            resetSubjectIds()
        },
        confirmCallback:function(parentId,subjectIds){//点击确认回调
            resetSubjectIds()
        }
    });
    /**
     * 重置专业ID串
     */
    function resetSubjectIds() {
        var _subjectIds = $.getAllSubjectIds("user-subject-context");
        $("input[name=subjectIds]").val(_subjectIds);
    }

    validateData();

    $("body").on('click','#selectCourse',function () {//选择课程
        selectCourse({
            selectType:"checkbox",
            type:1,
            callback:'selectCourseCallback'
        });
    }).on('click','#onBack',function (){
        onBackHistory();
    }).on('click','#submitUserFrom',function (){
        adminSaveUser();
    }).on('click','.deleteCourse',function () {
        var dataId =  $(this).parents('tr').data('id');
        var  obj = $(this).parents('tr');
        deleteSelectCourse(dataId,obj)
    });
});

/**
 * 取消
 */
function onBackHistory(){
    window.history.go(-1);
}

/**
 * 提交方法
 */
function adminSaveUser() {
    var params = $("#saveUserFrom").serializeJSON();
    params = JSON.stringify(params);
    $.ajax({
        url:baseloaction+"/admin/user/cus/ajaxSaveUser",
        data:params,
        type:"post",
        dataType:"json",
        contentType:'application/json;charset=utf-8',
        success:function (result) {
            if (result.success == true) {
                icModale(1,result.message,1,function () {
                    window.location.href=baseloaction+"/admin/user/cus/userList";
                });
            } else {
                icModale(0,result.message);
            }
        }
    });
}



/**
 * 选择课程回调
 * @param result 回调数据
 */
function selectCourseCallback(result) {
    if(result!=null && result.length>0){
        var courseIds = $('input[name="courseIds"]').val();
        if(courseIds==null||courseIds==''){
            courseIds =',';
        }
        $.each(result,function (index,course) {
            var courseName =course.courseName;
            var courseSubject=course.subject;
            var coursePrice =course.orPrice;
            if(courseIds.indexOf(","+course.id+",")==-1){
                courseIds +=course.id+',';
                $('#course-content').append(' <tr data-id="'+course.id+'">'+
                    '<td class="text-center">'+course.id+'</td>'+
                    '<td>'+courseName+'</td>'+
                    '<td>'+courseSubject+'</td>'+
                    '<td><span class="c-red">'+coursePrice+'</span></td>'+
                    '<td class="text-center"><a class="c-master deleteCourse"  href="javascript:void(0);">删除</a></td>'+
                    '</tr>');
            }
        });
        $('input[name="courseIds"]').val(courseIds);
    }
    parentId = 0;
}


function deleteSelectCourse(dataId,obj) {
    var courseIds=',';
    var courses = $("#courseIds").val();
    var courseId = courses.split(',');
    for(var i=0;i<courseId.length;i++){
        if(courseId[i]!=null&&courseId[i]!=''&&courseId[i]!=dataId){
            courseIds +=courseId[i]+",";
        }
    }
    if(courseIds.length>1){
        $("#courseIds").val(courseIds);
    }else{
        $("#courseIds").val("");
    }
    obj.remove();
}
